import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainFrame extends JFrame {

	private JPanel contentPane;
	private JTextArea textAreaIn;
	private JTextArea textAreaOut;
	private SparseMatrix sparseMatrix;
	private int[][] mat = { { 0, 0, 8, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 },
			{ 5, 0, 0, 0, 16, 0 }, { 0, 0, 18, 0, 0, 0 },
			{ 0, 0, 0, 9, 0, 0 } };

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("Matrix");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 456);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JScrollPane scrollPaneIn = new JScrollPane();
		scrollPaneIn.setBounds(14, 13, 404, 166);
		contentPane.add(scrollPaneIn);

		textAreaIn = new JTextArea();
		textAreaIn.setEditable(false);
		scrollPaneIn.setViewportView(textAreaIn);

		JScrollPane scrollPaneOut = new JScrollPane();
		scrollPaneOut.setBounds(14, 232, 404, 166);
		contentPane.add(scrollPaneOut);

		textAreaOut = new JTextArea();
		textAreaOut.setEditable(false);
		scrollPaneOut.setViewportView(textAreaOut);

		JButton btnTranspose = new JButton("Transpose");
		btnTranspose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				btnTransposeCall();
			}
		});
		btnTranspose.setBounds(159, 192, 113, 27);
		contentPane.add(btnTranspose);
		sparseMatrix = new SparseMatrix(mat);
		textAreaIn.setText(sparseMatrix.toString());
	}

	// 按钮点击监听
	protected void btnTransposeCall() {
		SparseMatrix tm = sparseMatrix.transpose();
		textAreaOut.setText(tm.toString());
	}
}
